/*
 *  $Id$
 *  Copyright [2010] [Panxiaobo] 
 *  Licensed under the Apache License, Version 2.0 (the "License"); 
 *  you may not use this file except in compliance with the License. 
 *  You may obtain a copy of the License at
 *  
 *     http://www.apache.org/licenses/LICENSE-2.0
 *     
 *  Unless required by applicable law or agreed to in writing, software 
 *  distributed under the License is distributed on an "AS IS" BASIS, 
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 *  See the License for the specific language governing permissions and 
 *  limitations under the License. 
 */
package pxb;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import pxb.openid.Association;
import pxb.openid.ns.Ax;
import pxb.openid.op.EncodedAssociationManager;
import pxb.openid.op.servlet.BaseOpenidProviderServlet;

@SuppressWarnings("serial")
public class OpenidProviderServlet extends BaseOpenidProviderServlet {

    /**
	 * 
	 */
    public OpenidProviderServlet() {
        super();

        setAssociationManager(new EncodedAssociationManager() {
        });
        if (associationManager instanceof EncodedAssociationManager) {
            ((EncodedAssociationManager) associationManager).setExpires(100);// 100s
        }
    }

    String nullSafe(String string) {
        if (string == null)
            return "";
        return string;
    }

    /*
     * (non-Javadoc)
     * 
     * @see pxb.openid.op.servlet.BaseOpenidProviderServlet#acceptAuth(java.lang.String, pxb.openid.Association,
     * java.util.Map, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
     */
    @Override
    public void acceptAuth(String serverURL, Association association, Map<String, String> parameters,
            HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String user = req.getParameter("user");
        if (user != null && !"".equals(user)) {
            String userURL = serverURL + "?u=" + user;
            Map<String, String> map = new HashMap<String, String>();
            if (Ax.hasAx(parameters)) {
                Ax ax = new Ax(parameters);
                ax.xCountry(nullSafe(req.getParameter("country"))).xFirstName(nullSafe(req.getParameter("firstname")))
                        .xLastName(nullSafe(req.getParameter("lastname")))
                        .xLanguage(nullSafe(req.getParameter("language"))).xEmail(nullSafe(req.getParameter("email")));
                map.putAll(ax.response().getMap());
            }

            successResponse(userURL, map, parameters, serverURL, association, req, resp);
        } else {
            parameters.remove("openid.mode");
            req.setAttribute("opnenid_parameters", parameters);
            req.getRequestDispatcher("/login.jsp").forward(req, resp);
        }
    }
}
